package com.warm.system.controller.system;


import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.ObjectUtil;
import com.warm.core.domain.AjaxResult;
import com.warm.excel.poi.utils.ExcelUtil;
import com.warm.log.annotation.Log;
import com.warm.log.enums.BusinessType;
import com.warm.mybatis.core.controller.BaseController;
import com.warm.mybatis.core.page.TableDataInfo;
import com.warm.pom.satoken.utils.LoginUtils;
import com.warm.system.api.domain.SysDictData;
import com.warm.system.service.ISysDictDataService;
import com.warm.system.service.ISysDictTypeService;
import org.noear.solon.annotation.*;
import org.noear.solon.core.handle.Context;
import org.noear.solon.validation.annotation.Valid;
import org.noear.solon.validation.annotation.Validated;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据字典信息
 *
 * @author warm
 */
@Valid
@Controller
@Mapping("/system/dict/data")
public class SysDictDataController extends BaseController {
    @Inject
    private ISysDictDataService dictDataService;

    @Inject
    private ISysDictTypeService dictTypeService;

    @SaCheckPermission("system:dict:list")
    @Get
    @Mapping("/list")
    public TableDataInfo<SysDictData> list(SysDictData dictData) {
        startPage();
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        return getDataTable(list);
    }

    @Log(title = "字典数据", businessType = BusinessType.EXPORT)
    @SaCheckPermission("system:dict:export")
    @Post
    @Mapping("/export")
    public void export(Context ctx, SysDictData dictData) {
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        ExcelUtil<SysDictData> util = new ExcelUtil<SysDictData>(SysDictData.class);
        util.exportExcel(ctx, list, "字典数据");
    }

    /**
     * 查询字典数据详细
     */
    @SaCheckPermission("system:dict:query")
    @Get
    @Mapping(value = "/{dictCode}")
    public AjaxResult getInfo(Long dictCode) {
        return success(dictDataService.selectDictDataById(dictCode));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @Get
    @Mapping(value = "/type/{dictType}")
    public AjaxResult dictType(String dictType) {
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
        if (ObjectUtil.isNull(data)) {
            data = new ArrayList<>();
        }
        return success(data);
    }

    /**
     * 新增字典类型
     */
    @SaCheckPermission("system:dict:add")
    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @Post
    @Mapping
    public AjaxResult add(@Validated SysDictData dict) {
        dict.setCreateBy(LoginUtils.getUsername());
        return toAjax(dictDataService.insertDictData(dict));
    }

    /**
     * 修改保存字典类型
     */
    @SaCheckPermission("system:dict:edit")
    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    @Put
    @Mapping
    public AjaxResult edit(@Validated SysDictData dict) {
        dict.setUpdateBy(LoginUtils.getUsername());
        return toAjax(dictDataService.updateDictData(dict));
    }

    /**
     * 删除字典类型
     */
    @SaCheckPermission("system:dict:remove")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @Delete
    @Mapping("/{dictCodes}")
    public AjaxResult remove(Long[] dictCodes) {
        dictDataService.deleteDictDataByIds(dictCodes);
        return success();
    }
}
